# rules.mak
# Created by tongzhilin on 2020.08.05.

################################ toolchain ################################
ifeq (.config, $(wildcard .config))
    include .config
endif

PROJ_PATH := $(shell pwd)

ifeq ($(CHIP_HI3559AV100), y)
    CHIP_NAME = hi3559av100
    CROSS = aarch64-himix100-linux-
	INFERENCE_ENGINE = nnie
endif

ifeq ($(ARCH_X86_32), y)
    CHIP_NAME = linux-32
	ifeq ($(WITH_MXNET), y)
		INFERENCE_ENGINE = mxnet
	endif
	ifeq ($(WITH_CAFFE), y)
		INFERENCE_ENGINE = caffe
	endif
endif

ifeq ($(ARCH_X86_64), y)
    CHIP_NAME = linux-64
	ifeq ($(WITH_MXNET), y)
		INFERENCE_ENGINE = mxnet
	endif
	ifeq ($(WITH_CAFFE), y)
		INFERENCE_ENGINE = caffe
	endif
endif

#ver
ifeq ($(V010), y)
	SDK_VER = v010
endif
ifeq ($(V020), y)
	SDK_VER = v020
endif

COMPILE_TIME="\"$(shell date +"%Y-%m-%d %H:%M:%S")\""
GIT_VERSION="\"$(shell git show -s --pretty=format:%H)\""

RELEASE_PATH = $(PROJ_PATH)
OUTPUT_OBJ_DIR = $(PROJ_PATH)/build/obj
OUTPUT_LIB_DIR = $(PROJ_PATH)/build/lib
OUTPUT_BIN_DIR = $(PROJ_PATH)/build/bin
OUTPUT_BIN_DIR_TMP = $(PROJ_PATH)/build
OUTPUT_INC_DIR = $(PROJ_PATH)/build/inc

PROJ_INC_DIR =  $(PROJ_PATH)/include
PROJ_SRC_DIR =  $(PROJ_PATH)/src
MODULE_ALG_HINNIE_DIR = $(PROJ_PATH)/src/alghinnie
MODULE_ALG_MXNET_DIR = $(PROJ_PATH)/src/algmxnet
MODULE_ALG_CAFFE_DIR = $(PROJ_PATH)/src/algcaffe
MODULE_ALG_COMMON_DIR = $(PROJ_PATH)/src/algcommon
MODULE_COMMON_DIR = $(PROJ_PATH)/src/common
MODULE_ALG_API_DIR = $(PROJ_PATH)/src/algapi
MODULE_ALG_BASIC_HINNIE_DIR = $(PROJ_PATH)/src/algbasic_hinnie
MODULE_ALG_BASIC_MXNET_DIR = $(PROJ_PATH)/src/algbasic_mxnet
MODULE_ALG_BASIC_CAFFE_DIR = $(PROJ_PATH)/src/algbasic_caffe
LIB_THIRD_DIR = $(PROJ_PATH)/third_party/$(CHIP_NAME)

CC      = $(CROSS)gcc
CPP     = $(CROSS)g++
LD      = $(CROSS)ld
AR      = $(CROSS)ar
RANLIB  = $(CROSS)ranlib
STRIP   = $(CROSS)strip

ARFLAGS = crs
PR      = 
#@ 

################################ option ################################
CFLAGS    =  -Wall -Werror
CXXFLAGS  =  -Wall -Werror
LDLIBS    = -lpthread -lm -ldl

ifeq ($(COMPILE_DEBUG), y)
	CFLAGS    += -ggdb3 -O0 -D__DEBUG
	CXXFLAGS  += -ggdb3 -O0 -D__DEBUG
else
	CFLAGS    += -O3 -D__RELEASE
	CXXFLAGS  += -O3 -D__RELEASE
endif

# option chip 
ifeq ($(CHIP_HI3559AV100), y)
	CFLAGS3559:= -DCOMPILE_TIME=$(COMPILE_TIME) -Dhi3559av100 -DHI_XXXX -DHI_RELEASE  \
		-DISP_V2 -lstdc++ -fno-aggressive-loop-optimizations       \
		-ffunction-sections -fdata-sections -fstack-protector -fPIC \
		-Wno-error=implicit-function-declaration -Wno-date-time -Wno-sign-compare \
		-fvisibility=hidden -fsigned-char
	CFLAGS3559 += -DOS_LINUX -DARCH_AARCH64 -DENABLE_NEON -DCHIP_HI3559AV100 
	CXXFLAGS  += $(CFLAGS3559)
	CFLAGS    += $(CFLAGS3559) \
		-Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -Wno-incompatible-pointer-types 
endif

ifeq ($(ARCH_X86_32), y)
	CXXFLAGS  += -fPIC -DOS_LINUX -DARCH_X86_32
	CFLAGS    += -fPIC -DOS_LINUX -DARCH_X86_32
endif

ifeq ($(ARCH_X86_64), y)
	CXXFLAGS  += -fPIC -DOS_LINUX -DARCH_X86_64
	CFLAGS    += -fPIC -DOS_LINUX -DARCH_X86_64
endif

################################ aideploy output ################################
TARGET_ALG_DEPLOY_LIB_NAME		= algdeploy
TARGET_LIB_ALG_DEPLOY_A			= $(OUTPUT_LIB_DIR)/lib$(TARGET_ALG_DEPLOY_LIB_NAME).a
TARGET_LIB_ALG_DEPLOY_SO		= $(OUTPUT_LIB_DIR)/lib$(TARGET_ALG_DEPLOY_LIB_NAME).so

################################ common output ################################
TARGET_COMMON_NAME				= common
TARGET_COMMON_BASIC_A			= $(MODULE_COMMON_DIR)/lib$(TARGET_COMMON_NAME).a
################################ alg common output ################################
TARGET_ALG_COMMON_NAME			= algcommon
TARGET_ALG_COMMON_BASIC_A		= $(MODULE_ALG_COMMON_DIR)/lib$(TARGET_ALG_COMMON_NAME).a

################################ nnie_basic output ################################
TARGET_NNIE_BASIC_NAME			= nniebasic
TARGET_NNIE_BASIC_A				= $(MODULE_ALG_BASIC_HINNIE_DIR)/lib$(TARGET_NNIE_BASIC_NAME).a
################################ mx_basic output ################################
TARGET_MX_BASIC_NAME			= mxbasic
TARGET_MX_BASIC_A				= $(MODULE_ALG_BASIC_MXNET_DIR)/lib$(TARGET_MX_BASIC_NAME).a
################################ caffe_basic output ################################
TARGET_CAFFE_BASIC_NAME			= caffebasic
TARGET_CAFFE_BASIC_A			= $(MODULE_ALG_BASIC_CAFFE_DIR)/lib$(TARGET_CAFFE_BASIC_NAME).a

TARGET_DEMO_DIR			= $(OUTPUT_BIN_DIR_TMP)

INC_FLAGS 			+= -I$(PROJ_INC_DIR)
INC_FLAGS 			+= -I$(PROJ_SRC_DIR)
INC_FLAGS 			+= -I$(MODULE_COMMON_DIR)/src
INC_FLAGS 			+= -I$(MODULE_COMMON_DIR)/inc
INC_FLAGS 			+= -I$(MODULE_ALG_COMMON_DIR)/src
INC_FLAGS 			+= -I$(MODULE_ALG_COMMON_DIR)/inc
INC_FLAGS 			+= -I$(MODULE_ALG_API_DIR)
ifeq ($(INFERENCE_ENGINE), nnie)
	INC_FLAGS 			+= -I$(MODULE_ALG_HINNIE_DIR)
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_HINNIE_DIR)/src
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_HINNIE_DIR)/inc
	CXXFLAGS  += -DINFERENCE_NNIE
	CFLAGS    += -DINFERENCE_NNIE
endif
ifeq ($(INFERENCE_ENGINE), mxnet)
	INC_FLAGS 			+= -I$(MODULE_ALG_MXNET_DIR)
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_MXNET_DIR)/src
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_MXNET_DIR)/inc
	CXXFLAGS  += -DINFERENCE_MXNET
	CFLAGS    += -DINFERENCE_MXNET
endif
ifeq ($(INFERENCE_ENGINE), caffe)
	INC_FLAGS 			+= -I$(MODULE_ALG_CAFFE_DIR)
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_CAFFE_DIR)/src
	INC_FLAGS 			+= -I$(MODULE_ALG_BASIC_CAFFE_DIR)/inc
	CXXFLAGS  += -DINFERENCE_CAFFE
	CFLAGS    += -DINFERENCE_CAFFE
endif
LDFLAGS  		 	+= -L$(OUTPUT_LIB_DIR)
################################ depond lib : third_party ################################
#hi sdk, include nnie
ifeq ($(CHIP_HI3559AV100), y)
	INC_FLAGS += -I$(LIB_THIRD_DIR)/hisdk/inc_$(SDK_VER)
	LDFLAGS   += -L$(LIB_THIRD_DIR)/hisdk/liball_$(SDK_VER)
	LDLIBS_THR+= -lsecurec -lmpi -l_hiae -lisp -l_hidehaze -l_hidrc -l_hildci \
				 -l_hiawb -lhdmi -live -lmd -lVoiceEngine -ldnvqe -lupvqe -lnnie -lgomp
endif
#mxnet
ifeq ($(INFERENCE_ENGINE), mxnet)
	INC_FLAGS += -I$(LIB_THIRD_DIR)/mxnet160/inc
	LDFLAGS	  += -L$(LIB_THIRD_DIR)/mxnet160/
	LDLIBS_THR+= -lmxnet
	LDFLAGS	  += -L/usr/local/lib/
	#LDFLAGS	  += -L/usr/lib/x86_64-linux-gnu/
	#LDFLAGS	  += -L/usr/local/cuda-10.1/targets/x86_64-linux/lib/
	#LDLIBS_THR+= -lcuda -lcudnn -lcublas -lcudart -lcufft -lcusolver -lcurand -lnvrtc -lnvToolsExt \
	#			 -lomp -lrt -lmkldnn -ldmlc
endif
#opencv
INC_FLAGS += -I$(LIB_THIRD_DIR)/opencv340/inc
LDFLAGS   += -L$(LIB_THIRD_DIR)/opencv340/
LDLIBS_THR+= -lcore -limgcodecs -limgproc 
ifeq ($(ARCH_X86_64), y)
	LDLIBS_THR+= -lhighgui
endif

#png
INC_FLAGS += -I$(LIB_THIRD_DIR)/png/inc
LDFLAGS   += -L$(LIB_THIRD_DIR)/png/
LDLIBS_THR+= -lpng

#zlib
INC_FLAGS += -I$(LIB_THIRD_DIR)/zlib/inc
LDFLAGS   += -L$(LIB_THIRD_DIR)/zlib/
LDLIBS_THR+= -lzlib

#libjpeg
INC_FLAGS += -I$(LIB_THIRD_DIR)/libjpeg/inc
LDFLAGS   += -L$(LIB_THIRD_DIR)/libjpeg/
LDLIBS_THR+= -ljpeg

LDLIBS    		+= $(LDLIBS_THR)
################################ export ################################
#output target
export TARGET_LIB_ALG_DEPLOY_A
export TARGET_LIB_ALG_DEPLOY_SO
export TARGET_DEMO_DIR

export TARGET_NNIE_BASIC_A
export TARGET_MX_BASIC_A
export TARGET_CAFFE_BASIC_A
export TARGET_COMMON_BASIC_A
export TARGET_ALG_COMMON_BASIC_A

#compile
export PR
export CROSS
export CC
export CPP
export LD
export AR
export STRIP
export RANLIB
export PROJ_PATH
export INC_FLAGS
export CFLAGS
export CXXFLAGS
export ARFLAGS
export LDFLAGS
export LDLIBS
export OUTPUT_OBJ_DIR
export OUTPUT_LIB_DIR
export OUTPUT_BIN_DIR
export OUTPUT_INC_DIR
export COMPILE_DEBUG
export COMPILE_RELEASE
export INFERENCE_ENGINE
export OUTPUT_SO